<template>
    <div class="pjzi">
        <p><input type="text" placeholder="添加评论" ref="inputval"></p>
        <p><button @click="fun()">查看评价</button><button @click="funx()">修改评价</button></p>
        <section v-for="(v,i) in this.$store.state.arr" :key="i" :user="v.user">
            <div class="head">
                <div class="left">
                    <span class="sp0"><van-icon name="user-circle-o" /></span>
                    <span class="sp0">{{this.$store.stale.arr.user}}</span>
                </div>
                <div class="right">
                    <van-rate v-model="value" />
                </div>
            </div>
            <main>
                <p>{{this.$store.state.arr.ptext}}</p>
                <img :src="this.$store.state.arr.image" alt="">
                <p><span>{{this.$store.state.arr.cit}}</span><span>{{this.$store.state.arr.font}}</span><span>{{this.$store.state.arr.timer}}</span></p>
            </main>
       </section>

    </div>
</template>

<script>
export default {
    data(){
        return {
             value: 3,
        }
    },
    created () {
        //在页面加载时读取localStorage里的状态信息
        if (localStorage.getItem("data") ) {
            //replaceState替换数据 Object.assign合并对象
            this.$store.replaceState(Object.assign({}, this.$store.state.arr,JSON.parse(localStorage.getItem("data"))))
        } 
        //在页面刷新时将vuex里的信息保存到localStorage里
        window.addEventListener("beforeunload",()=>{
            localStorage.setItem("data",JSON.stringify(this.$store.state.arr))
        })
      },
    methods:{
        fun(){
            this.$store.dispatch("uplink")
        },
        funx(){
             let newvals=Number(this.$refs.inputval.value)
            this.$store.commit("adds",newvals)
        }
    }
}
</script>

<style scoped>
    .pjzi{
        width: 100%;
        font-size: 0.12rem;
        box-sizing: border-box;
        padding: 0.1rem;
    }
    .head{
        width: 100%;
        display: flex;
        justify-content: space-between;
        margin: 0.1rem 0;
        padding: 0.1rem;
        border-bottom: 1px solid #ddd;
        box-sizing: border-box;
    }
    .sp0{
        margin: 0.1rem;
    }
    img{
        width: 0.8rem;
        height: 0.8rem;
    }
    p{
        padding: 0.1rem 0;
    }
    main{
        width: 100%;
        border-bottom: 1px solid #ddd;
        margin-bottom: 0.2rem;
    }
    button{
        outline: none;
        margin: 0.1rem;
        padding: 0.05rem 0.1rem;
    }
    input{
        margin: 0 auto;
        height: 0.3rem;
        width: 80%;
        outline: none;
        text-indent: 0.1rem;
    }
</style>